import Vue from 'vue'
import funDebug from 'fundebug-javascript'

let realName = () => {
  return localStorage.getItem('real_name')
}
// funDebug 配置
funDebug.apikey = '438dd768d03b697fa0904bd9a6211db8b52d2913f334dbc7a4749a97cd9970d3'
console.log('process.env.APP_VERSION', process.env.APP_VERSION)
funDebug.user = {
  name: 'real_name:' + realName(),
  email: ''
}
funDebug.appversion = process.env.APP_VERSION
funDebug.silentBehavior = false
funDebug.silentVideo = true
funDebug.silentDev = true
funDebug.silentConsole = true
funDebug.filters = [
  {
    type: /^unhandledrejection/
  },
  {
    message: /^(WeixinJSBridge is not defined|Script error\.)$/
  },
  { // 登录错误提示
    req: {
      url: /\/site\/login/
    },
    res: {
      statusText: /^Bad Request$/
    }
  },
  { // 未登录
    req: {
      method: /^(GET|HEAD)$/
    },
    res: {
      status: /^400$/,
      response: /"status":40000/
    }
  },
  { // 验证失败
    res: {
      status: /^(0|401)$/
    }
  }]

function formatComponentName (vm) {
  if (vm.$root === vm) return 'root'
  let name = vm._isVue ? (vm.$options && vm.$options.name) || (vm.$options && vm.$options._componentTag) : vm.name
  return (name ? 'component <' + name + '>' : 'anonymous component') + (vm._isVue && vm.$options && vm.$options.__file ? ' at ' + (vm.$options && vm.$options.__file) : '')
}

Vue.config.errorHandler = function (err, vm, info) {
  let componentName = formatComponentName(vm)
  let propsData = vm.$options && vm.$options.propsData
  funDebug.notifyError(err, {
    user: {
      name: 'real_name:' + realName()
    },
    metaData: {
      componentName: componentName,
      propsData: propsData,
      info: info,
      realName: realName()
    }
  })
}
